﻿/*=========================================================================================================================/
 * 
 * FILE: Layer.cs
 * AUTH: Matthew Baum
 * DATE: 10/28/2011
 * CHANGE: 10/28/2011
 * 
 * NOTE: This class holds a 2D array of textures to represent a BG layer (3 x 2)
 * 
 *========================================================================================================================
 *
 * BASED ON:
 * XNA Platformer Starter 3.1 Tutorial (Scrolling)
*-----------------------------------------------------------------------------*/


// System includes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// XNA includes
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Homebound
{
    class Layer
    {
        // PUBLIC MEMBERS
        // Layer textures
        public Texture2D[,] Textures { get; private set; }

        // Scrolling rate of the layer
        public float ScrollRate { get; private set; }

        // CONSTRUCTOR : load the set of textures that comprise this layer
        public Layer(ContentManager content, string basePath, float scrollRate)
        {
            // FOR NOW: Assumes each layer is 3 segments wide, by 2 segments high.
            Textures = new Texture2D[3,2];

            // Load all textures for a layer
            for (int i = 0; i != 6; ++i)
            {
                Textures[i % 3, i / 3] = content.Load<Texture2D>(basePath + "_" + i);
            }

            ScrollRate = scrollRate;
        }

        // Draw the background layer, translate according to "camera" X/Y offsets
        public void Draw(SpriteBatch spriteBatch, float cameraLeft, float cameraTop )
        {
            // Assume each segment is the same width
            int segmentWidth = Textures[0, 0].Width;
            int segmentHeight = Textures[0, 0].Height;

            // Calculate which segments (visible textures) to draw and how much to offset them.
            float x = cameraLeft * ScrollRate;
            float y = cameraTop * ScrollRate;

            int leftSegment = (int)Math.Floor(x / segmentWidth);
            int rightSegment = leftSegment + 1;

            x = (x / segmentWidth - leftSegment) * -segmentWidth;

            // Draw the BOTTOM LEFT visible texture
            spriteBatch.Draw(Textures[leftSegment % 3, 0], new Vector2(x, y + segmentHeight), Color.White);

            // Draw the BOTTOM RIGHT visible texture
            spriteBatch.Draw(Textures[rightSegment % 3, 0], new Vector2(x + segmentWidth, y + segmentHeight), Color.White);

            // Draw the TOP LEFT visible texture
            spriteBatch.Draw(Textures[leftSegment % 3, 1], new Vector2(x, y), Color.White);

            // Draw the TOP RIGHT visible texture
            spriteBatch.Draw(Textures[rightSegment % 3, 1], new Vector2(x + segmentWidth, y), Color.White);
        }
    }
}
